/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simplepushclient;

import java.awt.*;
import java.net.URL;
import javax.swing.*;
import javax.swing.event.*;


/**
 *
 * @author Filip
 */
public class VolumeSlider extends JFrame implements ChangeListener{
    private JSlider sli;
    private PusherTray parent;
    private Sender sender;
    private JLabel l = new JLabel("Volume: xxx");
    private Int i;
    private int wheelRotation;

    public VolumeSlider(PusherTray p, Int i) {
        this.i=i;
        this.parent=p;
        this.sender = new Sender(parent.getIp());
        this.sender.showTimeOut(false);
        Toolkit t = Toolkit.getDefaultToolkit();
        sli = new JSlider(JSlider.HORIZONTAL, 0 , 100, i.getIntValue());
        sli.createStandardLabels(10,0);
        sli.setPaintTicks(true);
        sli.addChangeListener(this);
        l.setText("Volume: "+sli.getValue());
        setTitle("Volume for "+sender.getDns().toString());
        l.setLabelFor(sli);
        l.setFont(new Font("sansserif", Font.BOLD, 16));
        BorderLayout bl = new BorderLayout(2, 2);
        setLayout(bl);
        add(l, bl.NORTH);
        add(sli, bl.CENTER);
        pack();
        sli.setLocation((t.getScreenSize().width/2)-250 , (t.getScreenSize().height/2)-40);
        setAlwaysOnTop(false);
        setResizable(false);
        setSize((getWidth()*3)/2, getHeight());
        try{
            URL u = new URL("http://img529.imageshack.us/img529/6775/33212910.gif");
            Image image = Toolkit.getDefaultToolkit().getImage(u);
            setIconImage(image);
        }catch(Exception e){}
        setFocusTraversalPolicyProvider(true);
        addMouseWheelListener(new java.awt.event.MouseWheelListener() {
         public void mouseWheelMoved(java.awt.event.MouseWheelEvent e) {
            int tmp = e.getWheelRotation();
            sender.setData("volume:"+(wheelRotation-tmp));
            wheelRotation-=tmp;
            sli.setValue(wheelRotation);
            sender.run();

         }
      });
    }

    public void stateChanged(ChangeEvent e) {
        sender.setData("volume:"+sli.getValue());
        sender.run();
        l.setText("Volume: "+sli.getValue());
        i.setIntValue(sli.getValue());
    }

    @Override
    protected void finalize() throws Throwable {
        sender.showTimeOut(true);
        super.finalize();
    }


}
